package de.sisoft.gcjack.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.sisoft.gcjack.R;
import de.sisoft.gcjack.view.sidenav.SideNavEntry;
import de.sisoft.gcjack.view.sidenav.SideNavItem;
import de.sisoft.gcjack.view.sidenav.SideNavSection;

public class SideNavListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<SideNavItem> sideNavItems;
    private LayoutInflater layoutInflater;

    public SideNavListAdapter(Context context,
	    ArrayList<SideNavItem> navDrawerItems) {
	this.context = context;
	this.sideNavItems = navDrawerItems;
	layoutInflater = (LayoutInflater) context
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
	return sideNavItems.size();
    }

    @Override
    public SideNavItem getItem(int position) {
	return sideNavItems.get(position);
    }

    @Override
    public long getItemId(int position) {
	return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//	if (convertView == null) {

	    final SideNavItem item = sideNavItems.get(position);
	    if (item != null) {
		if (item.isSection()) {
		    SideNavSection section = (SideNavSection) item;
		    convertView = layoutInflater.inflate(
			    R.layout.menu_list_section, null);

		    convertView.setOnClickListener(null);
		    convertView.setOnLongClickListener(null);
		    convertView.setLongClickable(false);

		    final TextView sectionView = (TextView) convertView
			    .findViewById(R.id.sidenav_section_title);
		    sectionView.setText(section.getTitle());

		} else {
		    SideNavEntry entry = (SideNavEntry) item;
		    convertView = layoutInflater.inflate(
			    R.layout.menu_list_entry, null);

		    ImageView imgIcon = (ImageView) convertView
			    .findViewById(R.id.sidenav_entry_icon);
		    TextView txtTitle = (TextView) convertView
			    .findViewById(R.id.sidenav_entry_title);
		    TextView txtCount = (TextView) convertView
			    .findViewById(R.id.sidenav_entry_counter);

		    imgIcon.setImageResource(entry.getIcon());
		    txtTitle.setText(entry.getTitle());

		    // displaying count
		    // check whether it set visible or not
		    if (entry.getCounterVisibility()) {
			txtCount.setText(entry.getCount());
		    } else {
			// hide the counter view
			txtCount.setVisibility(View.GONE);
		    }
		}
	    }
//	}
	return convertView;
    }
}
